package com.sanjiahantang.youjiaclient.module.money.presenters;

import android.content.Context;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.money.beans.BankCardBean;
import com.sanjiahantang.youjiaclient.module.money.models.IMoneyModel;
import com.sanjiahantang.youjiaclient.module.money.models.impl.MoneyModel;
import com.sanjiahantang.youjiaclient.module.money.views.MoneyView;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2018/3/23.
 */

public class MoneyPresenter extends BaseMvpPresenter<MoneyView> {
    private IMoneyModel moneyModel;

    public MoneyPresenter() {
        moneyModel = new MoneyModel();
    }


    //查看银行卡列表
    public void checkBankListInfo(Context context, final BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        moneyModel.checkBankListInfo(context, map, new CallBack<BankCardBean.CardList>() {

            @Override
            public void onSuccess(BankCardBean.CardList response, Context context, Object tag) {
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    if (response.getData() != null && response.getData().size() > 0) {
                        listener.onLoad(response.getData());
                    }
                }
                Utils.showToast(context, response.getMessage());
            }

            @Override
            public void onFailure(String message) {
                if (mView != null) mView.hideProgress();
            }
        });
    }

    public void updateDefaultBankCard(Context context, String mbciId, final BaseView.OnLoadListener listener) {
        Map<String, String> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put("mbciId", mbciId);
        map.put(KEY_PHONE, (String) SPUtils.get(context, SPConfig.PHONE, ""));
        moneyModel.updateDefaultBankCard(context, map, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                listener.onLoad(response);
            }

            @Override
            public void onFailure(String message) {
                listener.onLoad(message);
            }
        });
    }


    public void addBankCardInfo(final Context context,
                                ConditionBean.SelectorData bankData,
                                String bankNo,
                                String holder,
                                String idCard,
                                String phone,
                                ConditionBean.SelectorData typeData,
                                String cvv,
                                String date,
                                String openBank,
                                String strProvince,
                                String strCity,
                                final BaseView.OnLoadListener listener) {
        boolean isValid = true;
        String message = "";
        if (bankData == null) {
            isValid = false;
            message = "请选择银行名称";
        }

        if (isValid && TextUtils.isEmpty(bankNo)) {
            isValid = false;
            message = "请输入银行卡号";
        }

        if (isValid && TextUtils.isEmpty(holder)) {
            isValid = false;
            message = "请输入持卡人姓名";
        }

        if (isValid && TextUtils.isEmpty(idCard)) {
            isValid = false;
            message = "请输入身份证号";
        }

        if (isValid && TextUtils.isEmpty(phone)) {
            isValid = false;
            message = "请输入手机号";
        }

        if (isValid && typeData == null) {
            isValid = false;
            message = "请选择银行卡类型";
        }

        if (isValid && TextUtils.isEmpty(openBank)) {
            isValid = false;
            message = "请输入开户行名称";
        }

        if (isValid && TextUtils.isEmpty(strProvince)) {
            isValid = false;
            message = "请选择开户行地址";
        }

        if (isValid && TextUtils.equals("02", typeData.getDicValue())) {
            if (TextUtils.isEmpty(cvv)) {
                isValid = false;
                message = "请输入卡背面签字区编码后3位";
            }

            if (isValid && cvv.length() != 3) {
                isValid = false;
                message = "请输入卡背面签字区编码后3位";
            }

            if (isValid && TextUtils.isEmpty(date)) {
                isValid = false;
                message = "请选择信用卡有效期";
            }
        }

        if (isValid) {
            Map<String, Object> map = new HashMap<>();
            map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
            map.put("bankCardName", holder);
            map.put("bankCardNo", bankNo);
            map.put("bankCode", bankData.getDicValue());
            map.put("bankGenneralName", bankData.getDicName());
            map.put("bankPhone", phone);
            map.put("city", strCity);
            map.put("idCard", idCard);
            map.put("isDefault", "0");
            map.put("openingBankAddr", strProvince + " " + strCity);
            map.put("openingBankName", openBank);
            map.put("provcince", strProvince);
            map.put("bankCardType", typeData.getDicValue());
            if (TextUtils.equals("02", typeData.getDicValue())) {
                map.put("cvv", cvv);
                map.put("acctExpireDate", date);
            }

            if (mView == null) return;
            mView.showProgress();
            moneyModel.addBankInfo(context, map, new CallBack<StringBean>() {
                @Override
                public void onSuccess(StringBean response, Context context, Object tag) {
                    if (mView == null) return;
                    mView.hideProgress();
                    if (response.getCode() == RESPONSE_CODE_SUC) {
                        listener.onLoad(response);
                    } else {
                        Utils.showToast(context, response.getMessage());
                    }
                }

                @Override
                public void onFailure(String message) {
                    if (mView == null) return;
                    mView.hideProgress();
                    listener.onLoad(null);
                    Utils.showToast(context, message);
                }
            });
        } else {
            Utils.showToast(context, message);
        }
    }

    public void cardBindingSMS(final Context context, String bankCardNo, String smsCode, final BaseView.OnLoadListener listener) {
        if (TextUtils.isEmpty(smsCode)) {
            Utils.showToast(context, "验证码不可");
            return;
        }
        if (TextUtils.isEmpty(bankCardNo)) {
            Utils.showToast(context, "卡信息有误");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put("bankCardNo", bankCardNo);
        map.put("smsCode", smsCode);
        if (mView == null) return;
        mView.showProgress();
        moneyModel.cardBindingSMS(context, map, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response);
                } else {
                    Utils.showToast(context, response.getMessage());
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                listener.onLoad(null);
                Utils.showToast(context, message);
            }
        });
    }
}
